package com.fescoservice.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.directwebremoting.annotations.Param;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.spring.SpringCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fescoservice.config.PropertiesConfig;
import com.fescoservice.exception.FSException;
import com.fescoservice.model.BBS;
import com.fescoservice.model.BBSId;
import com.fescoservice.model.Customer;
import com.fescoservice.model.Operator;
import com.fescoservice.service.CategoryService;
import com.fescoservice.service.CustomerService;
import com.fescoservice.service.OperatorService;
import com.fescoservice.util.Constants;
import com.fescoservice.util.MiscUtil;
import com.fescoservice.util.PasswordUtil;

@Controller
@RequestMapping("/action/Login.htm")


public class LoginController {
	private static final Logger log = Logger.getLogger(LoginController.class);
	
	
	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private OperatorService operatorService;
	
	
	
	@RequestMapping(params="cmd=custLogin")
	public String custLogin(ModelMap map, HttpServletRequest request) throws Exception{
		String lang =(String)request.getSession().getAttribute("userLanguage");
		if(null==lang || lang.equals("")) 
		{
			lang=(String)request.getLocale().getLanguage();
			request.getSession().setAttribute("userLanguage",lang);
		}

		final String userId = request.getParameter("custName")==null?"":(String)request.getParameter("custName");
		final String userPass = request.getParameter("custBirth")==null?"":(String)request.getParameter("custBirth");

		
		
		List custList = customerService.queryCustLogin(userId);

		if(MiscUtil.isNullOrEmpty(custList)){
			throw new FSException("FS_ERROR_LOGIN_ERROR_CUST_NAME","");
		}
		List  customer = customerService.custLogin(userId,userPass);

		
		if(MiscUtil.isNullOrEmpty(customer)){
			throw new FSException("FS_ERROR_LOGIN_ERROR_CUST_BIRTH","");
		}
		//add
		
		Customer cust = (Customer)customer.get(0);

		map.addAttribute("Customer", cust);
		
		Long counts  = customerService.countApplications("","","",String.valueOf(cust.getCustId()));
		List list = customerService.getApplications("","","",String.valueOf(cust.getCustId()));
		map.addAttribute("applicationslist", list);
		map.addAttribute("counts", counts);
		//add end
		

		setCustSessionAttributes(customer,request);
		log.debug("~~~~~~~~language~~~~~~~~"+lang);
		return "customer/custhome";
	
	}
	
	@RequestMapping(params="cmd=hrLogin")
	public String hrLogin(ModelMap map, HttpServletRequest request) throws FSException{		
		String lang =(String)request.getSession().getAttribute("userLanguage");
		System.out.println(lang);
		if(null==lang || lang.equals("")) 
		{
			lang=(String)request.getLocale().getLanguage();
			request.getSession().setAttribute("userLanguage",lang);
		}
		
		
		final String userId = request.getParameter("operName")==null?"":(String)request.getParameter("operName");
		final String userPass = request.getParameter("operPassword")==null?"":(String)request.getParameter("operPassword");
		
		List operList = operatorService.queryHRLogin(userId,Constants.OPERATOR_ROLE_OUTER);
		if(MiscUtil.isNullOrEmpty(operList) || operList.size()==0){
			throw new FSException("FS_ERROR_LOGIN_ERROR_OPER_ID","");
		}
		String pw = PasswordUtil.encryptPassword(userId, userPass);
		List oper = operatorService.hrLogin(userId,pw,Constants.OPERATOR_ROLE_OUTER);
		if(MiscUtil.isNullOrEmpty(oper)){
			throw new FSException("FS_ERROR_LOGIN_ERROR_OPER_PASS","");
		}
		
		setOperSessionAttributes(oper,request);
		Operator	hr = (Operator)oper.get(0);
		getHRViewListModel(map,null,"",lang,hr.getOperCompanyId(),1);
		
		return "hr/hrhome";
	}

	
	@RequestMapping(params = "cmd=hrViewList")
	public String listCustomer4HR(String page,ModelMap map, HttpServletRequest request) throws FSException{
		//查询提条件
		String serchOptionSel = request.getParameter("SerchOptionSel")==null?"":(String)request.getParameter("SerchOptionSel");
		if(!MiscUtil.isNullOrEmpty(serchOptionSel)){			
			request.setAttribute("SerchOptionSel", serchOptionSel);
		}

		int pageNumber = Integer.parseInt((null==page || "".equals(page))?"1":page);
		String serchOptionValue  =  request.getParameter("SerchOptionValueSel")==null?"":(String)request.getParameter("SerchOptionValueSel");
		if(!MiscUtil.isNullOrEmpty(serchOptionValue)){			
			request.setAttribute("SerchOptionValueSel", serchOptionValue);
		}
		log.debug("~~~~~~~~Controller begin listCustomer~~~~~~~~");
		String language = request.getSession().getAttribute("userLanguage")==""?"Chinese":(String)request.getSession().getAttribute("userLanguage");
		//查询
		Integer companyId = (Integer)request.getSession().getAttribute("userCompanyId");
		getHRViewListModel(map,serchOptionSel,serchOptionValue,language,companyId,pageNumber);
		return "hr/hrhome";
	}
	
	
	@RequestMapping(params="cmd=operLogin")
	public String operLogin(ModelMap map, HttpServletRequest request) throws FSException{		
		String lang =(String)request.getSession().getAttribute("userLanguage");
		if(null==lang || lang.equals("")) 
		{
			lang=(String)request.getLocale().getLanguage();
			request.getSession().setAttribute("userLanguage",lang);
		}
		
		final String userId = request.getParameter("operName")==null?"":(String)request.getParameter("operName");
		final String userPass = request.getParameter("operPassword")==null?"":(String)request.getParameter("operPassword");
		System.out.println("PasswordUtil.encryptPassword("+userId+","+ userPass+"):"+PasswordUtil.encryptPassword(userId, userPass));
		List operList = operatorService.queryHRLogin(userId,Constants.OPERATOR_ROLE_INNER);
		if(MiscUtil.isNullOrEmpty(operList)){
			throw new FSException("FS_ERROR_LOGIN_ERROR_OPER_ID","");
		}

		List oper = operatorService.hrLogin(userId,PasswordUtil.encryptPassword(userId, userPass),Constants.OPERATOR_ROLE_INNER);
		
		if(MiscUtil.isNullOrEmpty(oper)){
			throw new FSException("FS_ERROR_LOGIN_ERROR_OPER_PASS","");
		}
		
		
		setOperSessionAttributes(oper,request);
		
		log.debug("~~~~~~~~language~~~~~~~~"+lang);
		return "common/manage";
	}
	
	private ModelMap getHRViewListModel(ModelMap map, String serchOptionSel,String serchOptionValue,String language,int companyId,int pageNumber) throws FSException{	
		/*
		if(null==language || "".equals(language.trim())){
			throw new FSException("FS_ERROR_SESSIONOUT","");
		}*/
		int first = PropertiesConfig.getInstance().getInt("ListSize.Customer")*(pageNumber-1);
		Long counts  = customerService.countCustomers4HR(serchOptionSel,serchOptionValue,companyId);
		List list = customerService.getCustomers4HR(serchOptionSel,serchOptionValue,companyId,first);
		map.addAttribute("customerlist", list);
		map.addAttribute("counts", counts);
		map.addAttribute("pageNumber", pageNumber);
		return map;
	}

	@RequestMapping(params="cmd=setLanguage")
	public String setLanguage(ModelMap map, HttpServletRequest request){		
		String lang =(String)request.getParameter("language");
		if(null==lang || lang.equals("")) lang=(String)request.getLocale().getLanguage();
		request.getSession().setAttribute("userLanguage",lang);
		log.debug("~~~~~~~~language~~~~~~~~"+lang);
		return "index";
	}
	
	
	
	@RequestMapping(params="cmd=logout")
	public String loginOut(ModelMap map, HttpServletRequest request) throws Exception{
		
		String lang =(String)request.getParameter("language");
		if(null==lang || lang.equals("")) lang=(String)request.getLocale().getLanguage();
		request.getSession().setAttribute("userLanguage",lang);
		
		request.getSession().removeAttribute("userId");
		request.getSession().removeAttribute("userRole");
		request.getSession().removeAttribute("userStatus");
		request.getSession().removeAttribute("userDept");
		request.getSession().removeAttribute("userName");
		request.getSession().removeAttribute("userCompanyId");
		request.getSession().invalidate();
		

		return "index";
	
	}
	

	
	public void setCustSessionAttributes(List userLogin,HttpServletRequest request){
		Customer	cust = (Customer)userLogin.get(0);
		request.getSession().setAttribute("userId", cust.getCustId());
		request.getSession().setAttribute("userRole","");
		request.getSession().setAttribute("userStatus", "");
		request.getSession().setAttribute("userDept", "");

	}
	
	public void setOperSessionAttributes(List userLogin,HttpServletRequest request){
		Operator	oper = (Operator)userLogin.get(0);
		request.getSession().setAttribute("userId", oper.getOperId());
		request.getSession().setAttribute("userRole",oper.getOperRoleId());
		request.getSession().setAttribute("userStatus", oper.getOperStatus());
		request.getSession().setAttribute("userDept", oper.getOperDeptId());
		request.getSession().setAttribute("userName", oper.getOperName());
		request.getSession().setAttribute("userCompanyId", oper.getOperCompanyId());
		
		

	}
	

	@RequestMapping(params = "cmd=modifyPasswordInit")
	public String modifyPasswordInit(ModelMap map) throws FSException{	
		
		//map.addAttribute("companyList", list);
		return "common/modifyPassword";
	}
}
